//
//  UIViewController+Add.m
//  PigMarket
//
//  Created by Felix on 2019/3/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "UIViewController+Add.h"
#import "UIView+Toast.h"
#import "LoginViewController.h"


@implementation UIViewController (Add)

- (void)handlingError:(NSError *)error {
    NSString *contentText = error.userInfo[NSLocalizedDescriptionKey];
    //[self toastWithContent:contentText];
}

- (void)handlingError:(NSError *)error errorDefaultHandling:(ErrorDefaultHandling)errorDefaultHandling {
    NSString *contentText = error.userInfo[NSLocalizedDescriptionKey];
    //[self toastWithContent:contentText];
}

- (void)hanglingError1:(NSString *)error{
    [self toastWithContent:error];
}

- (void)alertViewWithContent:(NSString *)contentText error:(NSError *)error {
    //清除用户信息
    [[UserInfoDataProvider sharedInstance] logoutPersonal];

    if (error.code == 0) {
        AlertDialogBuilder *alertDialogBuilder = [[AlertDialogBuilder alloc] init];
        AlertDialog *alertDialog = alertDialogBuilder.buildContentText(contentText)
        .buildCancelBtnTitle(@"退出软件")
        .buildCancelBtnBlockBlock(^(){ //退出软件
            exit(EXIT_SUCCESS);
        })
        .buildPositiveBtnTitle(@"立即升级")
        .buildPositiveBtnBlock(^(){ //goto appstore 更新
            NSString *urlStr = @"";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
            exit(EXIT_SUCCESS);
        })
        .buildAlertDialog;
        [alertDialog show];
    } else {
        @weakify(self)
        AlertDialogBuilder *alertDialogBuilder = [[AlertDialogBuilder alloc] init];
        AlertDialog *alertDialog = alertDialogBuilder.buildContentText(contentText)
        .buildPositiveBtnTitle(@"重新登录")
        .buildPositiveBtnBlock(^(){
            @strongify(self)
            
        })
        .buildAlertDialog;
        [alertDialog show];
    }
}


- (void)alertViewWithTitle:(NSString *)title
                   content:(NSString *)content
                  btnTitle:(NSString *)btnTitle
                  btnBlock:(nullable ButtonBlock )btnBlock {
    AlertDialogBuilder *alertDialogBuilder = [[AlertDialogBuilder alloc] init];
    AlertDialog *alertDialog = alertDialogBuilder.buildTitle(title)
    .buildContentText(content)
    .buildPositiveBtnTitle(btnTitle)
    .buildPositiveBtnBlock(btnBlock)
    .buildAlertDialog;
    [alertDialog show];
}
- (void)alertViewWithTitle:(NSString *)title
                   contentView:(UIView *)contentView
                  btnTitle:(NSString *)btnTitle
                  btnBlock:(nullable ButtonBlock )btnBlock {
    AlertDialogBuilder *alertDialogBuilder = [[AlertDialogBuilder alloc] init];
    AlertDialog *alertDialog = alertDialogBuilder.buildTitle(title)
    .buildContentView(contentView)
    .buildPositiveBtnTitle(btnTitle)
    .buildPositiveBtnBlock(btnBlock)
    .buildAlertDialog;
    [alertDialog show];
}


- (void)toastWithContent:(NSString *)content {
    CSToastManager.defaultDuration = 0.9;
    CSToastManager.defaultPosition = CSToastPositionCenter;
    [[UIApplication sharedApplication].keyWindow makeToast:content];
}


- (void)pushToViewController:(void(^)(void))handler {
    
    if ([UserInfoDataProvider sharedInstance].userInfo.isLogin) {
        handler();
    } else {
        LoginViewController *loginViewController = [[LoginViewController alloc] init];
        [loginViewController setLoginHandler:^{
            handler();
        }];
        [self.navigationController pushViewController:loginViewController animated:YES];
    }
}
@end
